﻿/**************************************************************
 *
 * 唯一标识：ab46d708-7df5-4fbc-80c4-aaf86719f6d7
 * 命名空间：Sgr.AuditLogs.Controllers
 * 创建时间：2023/8/20 15:58:24
 * 机器名称：DESKTOP-S0D075D
 * 创建者：antho
 * 电子邮箱：fengqinhua2016@163.com
 * 描述：
 *
 **************************************************************/

using Microsoft.AspNetCore.Mvc;
using Sgr.Application.ViewModels;
using Sgr.AspNetCore.ActionFilters.AuditLogs;
using Sgr.AspNetCore.ActionFilters.Permissions;
using Sgr.AuditLogs.Queries;
using Sgr.AuditLogs.ViewModels;

namespace Sgr.AuditLogs.Controllers
{
    /// <summary>
    /// 审计日志管理接口
    /// </summary>
    [Route("api/v1/sgr/audit-logs")]
    [ApiController]
    [Produces("application/json")]
    [Tags("LogOperate")]
    public class LogOperateController : ControllerBase
    {
        private readonly ILogOperateQueries _logOperateQueries;

        public LogOperateController(ILogOperateQueries logOperateQueries)
        {
            _logOperateQueries = logOperateQueries;
        }

        #region Queries

        /// <summary>
        /// 查询当前用户的审计日志列表
        /// </summary>
        /// <param name="request">分页及筛选参数（不需要传账号）</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <response code="200">查询成功</response>
        /// <response code="401">未经授权的访问</response>
        [CheckPermission]
        [HttpPost("my")]
        [ProducesResponseType(typeof(PagedResponse<LogOperateListModel>), StatusCodes.Status200OK)]
        [AuditLogActionFilter("查询当前用户审计日志列表")]
        public async Task<ActionResult<PagedResponse<LogOperateListModel>>> SearchMyAsync([FromBody] LogOperateSearchModel request, CancellationToken cancellationToken)
        {
            // 强制只查当前用户
            request.OnlyCurrentUser = true;

            var items = await _logOperateQueries.GetListAsync(request, cancellationToken);
            return Ok(items);
        }

        /// <summary>
        /// 分页查询审计日志列表
        /// </summary>
        /// <param name="request">查询条件</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <response code="200">查询成功</response>
        /// <response code="400">请求参数验证失败</response>
        /// <response code="401">未经授权的访问</response>
        [CheckPermission(Permissions.QueryAuditLogPermission)]
        [HttpPost("search")]
        [ProducesResponseType(typeof(PagedResponse<LogOperateListModel>), StatusCodes.Status200OK)]
        [AuditLogActionFilter("查询审计日志列表")]
        public async Task<ActionResult<PagedResponse<LogOperateListModel>>> SearchAsync([FromBody] LogOperateSearchModel request, CancellationToken cancellationToken)
        {
            var items = await _logOperateQueries.GetListAsync(request, cancellationToken);
            return Ok(items);
        }

        /// <summary>
        /// 获取审计日志详情
        /// </summary>
        /// <param name="id">审计日志ID</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>审计日志详细信息</returns>
        /// <response code="200">获取成功</response>
        /// <response code="401">未经授权的访问</response>
        /// <response code="404">指定的审计日志不存在</response>
        [CheckPermission(Permissions.QueryAuditLogPermission)]
        [HttpGet("{id:long:min(1)}")]
        [ProducesResponseType(typeof(LogOperateModel), StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        [AuditLogActionFilter("查询审计日志详情")]
        public async Task<ActionResult<LogOperateModel>> GetAsync([FromRoute] long id, CancellationToken cancellationToken)
        {
            var item = await _logOperateQueries.GetAsync(id, cancellationToken);
            return item == null ? NotFound() : Ok(item);
        }

        #endregion Queries
    }
}